package com.weighttracker.android;

import com.bappi.custom.activities.CustomActivity;
import com.bappi.utils.Constants;
import com.bappi.utils.Utils;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.EditText;

public class Tab2Activity1 extends CustomActivity {

	private EditText foodCaloriesField, foodUnitsField, activitiesWorkoutField;
	private SharedPreferences sharedPreference;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.layout_tab_2_screen_1);
			sharedPreference = PreferenceManager
					.getDefaultSharedPreferences(getParent());
			foodCaloriesField = (EditText) findViewById(R.id.food_calories_value);
			foodUnitsField = (EditText) findViewById(R.id.food_units_value);
			activitiesWorkoutField = (EditText) findViewById(R.id.activities_workouts_value);
			loadPreviousValue();

			findViewById(R.id.button_done).setOnClickListener(
					new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							try {
								String foodCalories = foodCaloriesField
										.getText().toString();
								String foodUnits = foodUnitsField.getText()
										.toString();
								String activitiesWorkout = activitiesWorkoutField
										.getText().toString();
								int foodCaloriesVal = 0;
								int activitiesWorkoutVal = 0;
								int foodUnitsVal = 0;
								if (foodCalories.length() > 0) {
									foodCaloriesVal = Integer
											.parseInt(foodCalories);
								}
								if (activitiesWorkout.length() > 0) {
									activitiesWorkoutVal = Integer
											.parseInt(activitiesWorkout);
								}
								if (foodUnits.length() > 0) {
									foodUnitsVal = Integer.parseInt(foodUnits);
								}
								if (((foodCaloriesVal > 0) || (foodUnitsVal>0) )
										&& (activitiesWorkoutVal > 0)) {
									sharedPreference
											.edit()
											.putInt(Constants.KEY_FOOD_CALORIES,
													foodCaloriesVal).commit();
									sharedPreference
											.edit()
											.putInt(Constants.KEY_FOOD_UNITS,
													foodUnitsVal).commit();
									sharedPreference
											.edit()
											.putInt(Constants.KEY_ACTIVITIES_WORKOUT,
													activitiesWorkoutVal)
											.commit();
									Utils.showAlertMessage(
											getParent(),
											getString(R.string.message_saved_success));
								}
								else {
									Utils.showAlertMessage(
											getParent(),
											getString(R.string.message_food_and_workout_goal_empty));
								}
							} catch (Exception e) {
								e.printStackTrace();
							}

						}
					});

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void loadPreviousValue() throws Exception {
		int val = sharedPreference.getInt(Constants.KEY_FOOD_CALORIES, 0);
		if (val != 0) {
			foodCaloriesField.setText(Integer.toString(val));
		}
		val = sharedPreference.getInt(Constants.KEY_FOOD_UNITS, 0);
		if (val != 0) {
			foodUnitsField.setText(Integer.toString(val));
		}
		val = sharedPreference.getInt(Constants.KEY_ACTIVITIES_WORKOUT, 0);
		if (val != 0) {
			activitiesWorkoutField.setText(Integer.toString(val));
		}
	}
}
